// pages/score/score.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    year: ['请选择年份'],
    index2: 0,
    province: ['请选择省份'],
    province_id: '',
    index3: 0,
    school: ['请选择学校'],
    index4: 0,
    class: ['请选择学科门类'],
    class_code: '',
    index5: 0,
    faculty: ['请选择学院名称(选填)'],
    index6: 0,
    course: ['请选择专业名称(选填)'],
    course_code: '',
    page: 200
  },
  ggxq(e) {
    const _ = this;
    console.log(e)
    let link = e.currentTarget.dataset.link
    wx.navigateTo({
      url: '../result/result?link=' + link,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _ = this;
    wx.request({ //发送请求
      url: 'https://www.kaoyan365.cn/index.php?m=business&c=dudao_bus&a=scoreIndex', //接收url
      data: {}, //接收参数
      success(res) { //返回
        console.log(res)
        var list = []
        list.push('请选择年份')
        for (var i in res.data.year) { //循环写入下来框
          list.push(res.data.year[i].year)
        }
        var list2 = []
        list2.push('请选择省份')
        for (var i in res.data.province) { //循环写入下来框
          list2.push(res.data.province[i].p_name)
        }
        _.setData({
          year: list,
          province: res.data.province,
          p_name: list2
        })
      }
    })
    console.log(options);
    _.setData({
      area: options.area,
      agent: options.agent
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }
  },
  bindPickerChange: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('year发送选择改变，携带值为', _.data.year[num]);
    this.setData({
      index: e.detail.value,
      index2: 0,
    })
    // wx.request({ //发送请求
    //   url: 'http://www.kaoyan365.cn/index.php?m=business&c=dudao_bus&a=scoreIndex&type=5', //接收url
    //   method: 'POST',
    //   data: {
    //     year: _.data.year[_.data.index]
    //   }, //接收参数
    //   success(res) {
    //     console.log(res)
    //     var list = [];
    //     for (var i in res.data) { //循环写入下来框
    //       list.push(res.data[i].p_name)
    //     }
    //     var list2 = [];
    //     for (var i in res.data) { //循环写入下来框
    //       list2.push(res.data[i].id)
    //     }
    //     _.setData({
    //       province: list,
    //       province_id: list2
    //     });
    //   }
    // })
  },
  bindPickerChange2: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('province发送选择改变，携带值为', _.data.p_name[num]);
    this.setData({
      index2: e.detail.value
    })

    var arr = _.data.province;
    var r = arr.filter(item=>item.p_name === _.data.p_name[num])
    _.setData({
      r:r[0].id
    })
    wx.request({ //发送请求
      url: 'https://api.kaoyan365.cn/index.php/api/kaoyanapi/requestpost/redirect/fsfsx_index/?type=1', //接收url
      method: 'POST',
      data: {
        year: _.data.year[_.data.index],
        province_id: r[0].id
      }, //接收参数
      success(res) {
        console.log(res)
        var list = [];
        for (var i in res.data) { //循环写入下来框
          list.push(res.data[i].school)
        }
        _.setData({
          school: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('school发送选择改变，携带值为', _.data.school[num]);
    this.setData({
      index3: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://api.kaoyan365.cn/index.php/api/kaoyanapi/requestpost/redirect/fsfsx_index/?type=2', //接收url
      method: 'POST',
      data: {
        year: _.data.year[_.data.index],
        province_id:_.data.r,
        school: _.data.school[_.data.index3],
      }, //接收参数
      success(res) {

        var list = [];
        var list2 = [];
        // list.push('请选择学科门类')
        // list2.push('请选择学科门类')
        for (var i in res.data) { //循环写入下来框
          list.push(res.data[i].class_name)
        }
       
        for (var i in res.data) { //循环写入下来框
          list2.push(res.data[i].class_code)
        }
        console.log(res.data)
        _.setData({
          class: list,
          class_code: list2,
        });
        console.log(_.data.class)
        console.log(_.data.class_code)
      }
    })
  },
  bindPickerChange4: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('xkml发送选择改变，携带值为', _.data.class[num]);
    this.setData({
      index4: e.detail.value
    })
  
    wx.request({ //发送请求
      url: 'https://api.kaoyan365.cn/index.php/api/kaoyanapi/requestpost/redirect/fsfsx_index/?type=3', //接收url
      method: 'POST',
      data: {
        year: _.data.year[_.data.index],
        province_id:_.data.r,
        school: _.data.school[_.data.index3],
        class_code: _.data.class_code[_.data.index4],
      }, //接收参数
      success(res) {
        console.log(res)
        var list = [];
        list.push('请选择学院名称(选填)')
        for (var i in res.data.faculty) { //循环写入下来框
          list.push(res.data.faculty[i].faculty)
        }
        _.setData({
          faculty: list
        });
      }
    })
  },
  bindPickerChange5: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('faculty发送选择改变，携带值为', _.data.faculty[num]);
    this.setData({
      index5: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://api.kaoyan365.cn/index.php/api/kaoyanapi/requestpost/redirect/fsfsx_index/?type=4', //接收url
      method: 'POST',
      data: {
        year: _.data.year[_.data.index],
        province_id: _.data.province_id[_.data.index2],
        school: _.data.school[_.data.index3],
        class_code: _.data.class_code[_.data.index4],
        faculty: _.data.faculty[_.data.index5],
      }, //接收参数
      success(res) {
        var list = [];
        // list.push('请选择专业名称(选填)')
        for (var i in res.data) { //循环写入下来框
          list.push(res.data[i].course_name)
        }
        var list2 = [];
        for (var i in res.data) { //循环写入下来框
          list2.push(res.data[i].course_code)
        }
        _.setData({
          course: list,
          course_code: list2,
        });
      }
    })
  },
  bindPickerChange6: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('course发送选择改变，携带值为', _.data.course[num]);
    this.setData({
      index6: e.detail.value
    })
  },

  tijiao: function (e) {
    var _ = this;
    var phone = wx.getStorageSync('23fsxphone');
    var year = _.data.year[_.data.index];
    var province_id = _.data.r;
    var school = _.data.school[_.data.index3];
    var class_code = _.data.class_code[_.data.index4];
    var faculty = _.data.faculty[_.data.index5];
    var course_code = _.data.course_code[_.data.index6];
    if(faculty == '请选择学院名称(选填)'){
      faculty = '';
      course_code=''
    }
    if (phone) {
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/chaxun/writelogs/?actid=48545&callback=?",
        data: {
          phone: phone,
          year: year,
          school: school,
          faculty: faculty,
          
        },
      })

      wx.request({ //发送请求
        url: "https://www.kaoyan365.cn/index.php?m=business&c=dudao_bus&a=scoreline",
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          year: year,
          province_id: province_id,
          school: school,
          class_code: class_code,
          faculty: faculty,
          course_code: course_code
        },
        success(res) { //返回

          console.log(res)
          wx.setStorageSync('fsxlist', res.data.data)
          _.setData({
            lists: res.data.data
          })
          // wx.navigateTo({
          //   url: '../result/result',
          // })
        }
      })
    } else {
      wx.showToast({
        title: '请先登录',
        duration: 2000,
        icon: 'none'
      })
      _.setData({
        fixed: 1
      })
    }
  },

  getPhoneNumber(e) {
    var _ = this;
    var area = _.data.area;
    var agent = _.data.agent;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      wx.setStorageSync('23fsxphone', res.result)
      console.log(res.result)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/chaxun/register?actid=48545&callback=?', //接收url
        data: {
          phone: res.result,
          isagree: true,
          area: area,
          agent: agent,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          _.setData({
            fixed: !1
          })
        }
      })

      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 25,
          mobile: res.result,
          areaCode: _.data.area,
          agent: _.data.agent,
          pageId: _.data.page
        },
        success: res => {
          console.log(res);
        }
      })
    }).catch(err => {
      console.error(err);
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})